package com.dlc.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.dlc.shop.bean.dto.ThirdApiAbutmentUserDto;
import com.dlc.shop.bean.event.LevelUpEvent;
import com.dlc.shop.bean.event.allinpay.CreateAllinpayMemberEvent;
import com.dlc.shop.bean.model.User;
import com.dlc.shop.bean.model.UserExtension;
import com.dlc.shop.common.bean.PaySettlementConfig;
import com.dlc.shop.common.config.Constant;
import com.dlc.shop.common.exception.YamiShopBindException;
import com.dlc.shop.dao.UserExtensionMapper;
import com.dlc.shop.service.SysConfigService;
import com.dlc.shop.service.ThirdApiAbutmenService;
import com.dlc.shop.service.UserService;
import lombok.AllArgsConstructor;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.Objects;

/**
 * @author xxw
 * @version 1.0
 * @description: 第三方接口对接
 * @since 2023/12/18 15:03
 */
@Service
@AllArgsConstructor
public class ThirdApiAbutmentServiceImpl implements ThirdApiAbutmenService {
    private final UserService userService;
    private final UserExtensionMapper userExtensionMapper;
    private final ApplicationContext applicationContext;
    private final SysConfigService sysConfigService;

    @Override
    public void regiserUser(ThirdApiAbutmentUserDto thirdApiAbutmentUserDto) {
        // 校验用户id是否重复
        long count = userService.count(new LambdaQueryWrapper<User>()
                .eq(User::getUserId, thirdApiAbutmentUserDto.getUserId()));
        if (count > 0) {
            throw new YamiShopBindException("用户id在系统中已存在，请更换!");
        }
        User user = new User();
        user.setUserId(thirdApiAbutmentUserDto.getUserId());
        user.setNickName(thirdApiAbutmentUserDto.getNickName());
        user.setModifyTime(new Date());
        user.setUserRegtime(new Date());
        user.setLevel(1);
        user.setLevelType(1);
        user.setLevelType(0);
        // 默认开启商品个性化推荐
        user.setProdRecommendation(1);
        userService.save(user);
        // 创建用户拓展信息
        UserExtension userExtension = new UserExtension();
        userExtension.setBalance(0.0);
        userExtension.setTotalBalance(0.0);
        userExtension.setGrowth(0);
        userExtension.setLevel(1);
        userExtension.setLevelType(0);
        userExtension.setScore(0L);
        userExtension.setUpdateTime(new Date());
        userExtension.setVersion(0);
        userExtension.setSignDay(0);
        userExtension.setUserId(thirdApiAbutmentUserDto.getUserId());
        userExtension.setAllinpayBalance(0.0);
        userExtension.setAllinpayTotalBalance(0.0);
        userExtension.setAllinpayRealNameSet(0);
        userExtension.setAllinpayPhoneBind(0);
        userExtension.setAllinpayProtocolSign(0);
        userExtension.setAllinpayPayPwdSet(0);
        userExtension.setAllinpayPayAcctBind(0);
        userExtensionMapper.insert(userExtension);
        //用户注册成功后发送等级提升事件
        applicationContext.publishEvent(new LevelUpEvent(userExtension,1,0, Constant.PLATFORM_SHOP_ID, 1));

        // 若开启通联，则同步创建通联个人会员
        PaySettlementConfig paySettlementConfig = sysConfigService.getSysConfigObject(Constant.PAY_SETTLEMENT_CONFIG, PaySettlementConfig.class);
        if (Objects.equals(paySettlementConfig.getPaySettlementType(), 1)) {
            CreateAllinpayMemberEvent event = new CreateAllinpayMemberEvent();
            event.setUserIds(Collections.singletonList(user.getUserId()));
            applicationContext.publishEvent(event);
        }
    }

    @Override
    public void updateUser(ThirdApiAbutmentUserDto thirdApiAbutmentUserDto) {
        userService.update(new LambdaUpdateWrapper<User>()
                .set(User::getNickName, thirdApiAbutmentUserDto.getNickName())
                .eq(User::getUserId, thirdApiAbutmentUserDto.getUserId()));
    }

    @Override
    public void disableUser(String userId) {
        userService.update(new LambdaUpdateWrapper<User>()
                .set(User::getStatus, 0)
                .eq(User::getUserId, userId));
    }
}
